package com.naiterui.ehp.bs.b2c.address.controller;

import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bs.b2c.address.exception.AddrExceptionCodes;
import com.naiterui.ehp.bs.b2c.address.service.IShippingInfoService;
import com.naiterui.ehp.bs.b2c.address.vo.ShippingInfoDetailVO;
import com.naiterui.ehp.bs.b2c.address.vo.ShippingInfoParamVO;
import com.naiterui.ehp.bs.b2c.address.vo.ShippingInfoVO;
import com.naiterui.ehp.bs.b2c.common.util.UserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 地址簿控制层
 *
 * @author guoyongxiang
 * @date 2019-01-10 18:17
 * @since 1.0.0
 */
@Validated
@Api(tags = SwaggerTag.PATIENT + SwaggerTag.ADDRESES)
@RequestMapping("b2c/addreses")
@RestController
public class AddressController {

    private static final Logger LOGGER = LoggerFactory.getLogger(AddressController.class);

    @Autowired
    private IShippingInfoService shippingInfoService;

    @ApiOperation(value = "地址列表")
    @GetMapping
    public ResponseVO<List<ShippingInfoVO>> list() {
        Long userId = UserUtils.getUser().getPatientId();
        LOGGER.info("获取地址列表 userId:{}", userId);

        List<ShippingInfoVO> shippingInfoVOList = this.shippingInfoService.getShippingInfoList(userId);
        return PublicService.returnResponseVO(shippingInfoVOList);
    }

    @ApiOperation(value = "地址详情")
    @ApiImplicitParam(name = "shippingInfoId", value = "地址ID", paramType = "path", dataType = "long", required = true)
    @GetMapping("detail/{shippingInfoId}")
    public ResponseVO<ShippingInfoDetailVO> detial(@PathVariable Long shippingInfoId) {
        Long userId = UserUtils.getUser().getPatientId();
        LOGGER.info("获取地址详情 userId:{}, shippingInfoId:{}", userId, shippingInfoId);

        ShippingInfoDetailVO shippingInfoVO = this.shippingInfoService.getShippingInfo(userId, shippingInfoId);
        return PublicService.returnResponseVO(shippingInfoVO);
    }


    @ApiOperation(value = "保存地址", notes = "新增、修改、设置默认地址")
    @PostMapping("save")
    public ResponseVO<ShippingInfoVO> save(@RequestBody ShippingInfoParamVO vo) throws BusinessException {
        Long userId = UserUtils.getUser().getPatientId();
        LOGGER.info("保存地址 userId:{}, param:{}", userId, vo.toString());

        ShippingInfoVO shippingInfoVO = this.shippingInfoService.saveShippingInfo(userId, vo);
        return PublicService.returnResponseVO(shippingInfoVO);
    }

    @ApiOperation(value = "删除地址")
    @ApiImplicitParam(name = "shippingInfoId", value = "地址ID", paramType = "path", dataType = "long", required = true)
    @PostMapping("delete/{shippingInfoId}")
    public ResponseVO<Void> delete(@PathVariable Long shippingInfoId) {
        Long userId = UserUtils.getUser().getPatientId();
        LOGGER.info("删除地址 userId:{}, shippingInfoId:{}", userId, shippingInfoId);

        this.shippingInfoService.deleteShippingInfo(userId, shippingInfoId);
        return PublicService.returnResponseVO(AddrExceptionCodes.SUCCESS);
    }


}
